/** 
 * @(#)FreemarkerUtils.java 2013-5-22
 * 
 * Copyright (c) 1995-2013 Wonders Information Co.,Ltd. 
 * 1518 Lianhang Rd,Shanghai 201112.P.R.C.
 * All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Wonders Group.
 * (Social Security Department). You shall not disclose such
 * Confidential Information and shall use it only in accordance with 
 * the terms of the license agreement you entered into with Wonders Group. 
 *
 * Distributable under GNU LGPL license by gnu.org
 */

package com.xieguoking.tools;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * @author xieguojun
 * @version $Revision$ 2013-5-22
 * @author ($Date$ modification by $Author$)
 * @since 1.0
 */
public class FreemarkerUtils {

	private static TemplateLoader loader;

	public static String generateText(String name, Map<String, Object> params) {

		Configuration cfg = new Configuration();
		cfg.setTemplateLoader(loader);
		StringWriter sw = new StringWriter();
		try {
			Template template = cfg.getTemplate(name);
			template.process(params, sw);
			return sw.toString();
		}
		catch (IOException e) {
			throw new ToolsException(e);
		}
		catch (TemplateException e) {
			throw new ToolsException(e);
		}
	}

	public static void setTemplateLoader(TemplateLoader loader) {
		FreemarkerUtils.loader = loader;
	}

}
